/* @(#)GSOrderDeptInfoProcessor.java    
 *
 * Project: GSdevelop
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   owen         2006-2-16
 *   Jenny Li     2006-3-16   新增方法getOrderDeptInfoOfOper
 *   Jenny Li     2006-3-18   新增方法getOrderDeptInfoOfBusOper
 *   Jenny Li     2006-6-8    新增方法getOrderDeptOfBusOper
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.business;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;

import com.hitrust.gs.bean.GSOrderDeptInfoBean;
import com.hitrust.gs.dao.GSOrderDeptInfoDAO;
import com.hitrust.gs.dao.GSOrganizationDAO;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.service.GSBaseProcesser;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.vo.GSOrderDeptInfoVO;

/**
 *  订货单位常用联系方式
 */
public class GSOrderDeptInfoProcessor implements GSBaseProcesser {
    /**
     * 通过订货单位编号查询订货单位的常用联系方式
     * 
     * @param aOrgId
     *            订货单位编号
     * @throws GSException
     * 
     * return ArrayList
     */
    public static ArrayList queryOrderDeptInfo(String aOrgId,String aAddr)throws GSException{
        ArrayList tArrayList=new ArrayList();
        GSOrderDeptInfoDAO tGSOrderDeptInfoDAO=new GSOrderDeptInfoDAO();
        try {
            //通过订货单位编号查询所有机构信息
            tArrayList=tGSOrderDeptInfoDAO.queryOrderDeptInfo(aOrgId,aAddr);
            
        } catch (GSException e) {
            GSLogWriter.error("GSOrderDeptInfoProcessor GSException: " +e.toString());
        }
        return tArrayList;
    }
    
    /**
     * 通过订货单位编号查询订货单位的常用联系方式
     * 
     * @param aOrgId
     *            订货单位编号
     * @throws GSException
     * 
     * return ArrayList
     */
    public static ArrayList getOrderDeptInfo(String aOrgId)throws GSException{
        ArrayList tArrayList=new ArrayList();
        GSOrderDeptInfoDAO tGSOrderDeptInfoDAO=new GSOrderDeptInfoDAO();
        try {
            //通过订货单位编号查询所有机构信息
            tArrayList=tGSOrderDeptInfoDAO.getOrderDeptInfo(aOrgId);
            
        } catch (GSException e) {
            GSLogWriter.error("GSOrderDeptInfoProcessor GSException: " +e.toString());
        }
        return tArrayList;
    }
    
    /**
     * 根据订货单位编号是否存在
     * @throws CUPSException          
     */
    public static void permitAdd(String aOrgId)throws GSException {
        GSOrderDeptInfoDAO tGSOrderDeptInfoDAO=new GSOrderDeptInfoDAO();
        //判断订货单位编号是否存在
        if (tGSOrderDeptInfoDAO.isExist(aOrgId)) {
                  throw new GSException("该订货单位编号已经被添加，不能在添加。");
            }
    }
    
    /**
     * 新增订货单位的常用联系方式
     * 
     */
    public static void addTask(GSOrderDeptInfoBean aGSOrderDeptInfoBean)throws GSException {
        GSOrganizationDAO tGSOrganizationDAO = new GSOrganizationDAO();
        GSOrderDeptInfoDAO tGSOrderDeptInfoDAO=new GSOrderDeptInfoDAO();
            //判断组织机构是否存在
            if (tGSOrganizationDAO.isExist(aGSOrderDeptInfoBean.getGs_org_id())==false) {
                      throw new GSException("该组织机构不存在。");
                }
            //int i=Integer.parseInt(aGSOrderDeptInfoBean.getGs_deliver_goods_addr_id());
            ArrayList tArrayList=GSOrderDeptInfoProcessor.queryOrderDeptInfo(aGSOrderDeptInfoBean.getGs_org_id(),"");
            int i=tArrayList.size();
            if(i>29){
                throw new GSException("添加组织机构已经超过30个不能在添加了。");
            }
            //添加订货单位的常用联系方式
            if (tGSOrderDeptInfoDAO.add(aGSOrderDeptInfoBean) == false) {
                      throw new GSException("");
                }
        }
    
    /**
     * 通过机构编号得到详细信息
     * 
     * @param String aOrgId  订货单位编号
     * @param String aAddrId 送货地址编号
     * 
     * return GSOrderDeptInfoBean
     * @throws GSException          
     */
    public static GSOrderDeptInfoBean queryOrderDeptInfoOne(String aOrgId,String aAddrId)throws GSException{
        GSOrderDeptInfoDAO tGSOrderDeptInfoDAO = new GSOrderDeptInfoDAO();
        GSOrderDeptInfoBean tGSOrderDeptInfoBean=new GSOrderDeptInfoBean();  
        try {
            //通过机构编号和机构名称查询所有机构信息
            tGSOrderDeptInfoBean=tGSOrderDeptInfoDAO.queryOrderDeptInfoOne(aOrgId,aAddrId);
        } catch (GSException e) {
            GSLogWriter.error("GSOrderDeptInfoProcessor GSException: " +e.toString());
        }
        return tGSOrderDeptInfoBean;
    }
    
    /**
     * 修改订货单位的常用联系方式
     * 
     * @param aMerchantTempBean
     *           
     * throws CUPSException
     */
    public static void modifyTask(GSOrderDeptInfoBean aGSOrderDeptInfoBean) throws GSException{
        GSOrderDeptInfoDAO tGSOrderDeptInfoDAO=new GSOrderDeptInfoDAO();
       
        //修改订货单位的常用联系方式
        tGSOrderDeptInfoDAO.modify(aGSOrderDeptInfoBean);
    }
    
    /**
     * 删除订货单位的常用联系方式
     * 
     * @param aSerialNo 
     *              序列号
     * @param aUpdateNo
     *             修改顺序号
     * throws CUPSException
     */
    public static void withdrawTask(String aOrgId,String aAddrId)throws GSException{
        GSOrderDeptInfoDAO tGSOrderDeptInfoDAO=new GSOrderDeptInfoDAO();
      
     //删除订货单位的常用联系方式
          if(tGSOrderDeptInfoDAO.deleteOne(aOrgId,aAddrId)==false){
                throw new GSException("");
            }
    }
    /**
     * 运营公司操作员取得当前组织和下级组织及对应送货地址信息
     * @param aOrgId            组织编码     
     * @return
     * @throws GSException
     */
    public HashMap getOrderDeptInfoOfOper(String aOrgId) throws GSException{
        HashMap tMap = new HashMap();
        ArrayList tRs = new ArrayList();
        Connection tConn = null;
        GSOrderDeptInfoDAO tOrderDeptDAO = new GSOrderDeptInfoDAO();
        try{
            tConn = tOrderDeptDAO.getConnetion();
            
            if(aOrgId.substring(0,2).equals("00")){
                //操作员属于巴士物资时，可操作所有运营公司及修理厂
                tRs = tOrderDeptDAO.queryAllOrderDeptInfoOfSubOrg(tConn);
            }else{
                //查询操作员所属订货单位及送货地址
                tRs = tOrderDeptDAO.queryOrderDeptInfoOfOrg(tConn,aOrgId);
                //查询操作员所属单位下级单位及送货地址
                tRs.addAll(tOrderDeptDAO.queryOrderDeptInfoOfOperSubOrg(tConn,aOrgId));
            }
            if(tRs.size() > 0){
                String temp = "";
                ArrayList tList1 = new ArrayList();
                ArrayList tList2 = null;
                for(int i=0; i<tRs.size(); i++){
                    GSOrderDeptInfoVO tVO = (GSOrderDeptInfoVO)tRs.get(i);
                    if(temp.equals(tVO.orgId)){
                        //取出同一个订货单位的不同送货地址封装到ArrayList容器中
                        String[] tDelGoodsAddr = new String[2];                        
                        tDelGoodsAddr[0] = tVO.deliverGoodsAddrId;
                        tDelGoodsAddr[1] = tVO.deliverGoodsAddrName;
                        tList2.add(tDelGoodsAddr);
                    }else{
                        //取出不同订货单位封装到ArrayList容器中
                        String[] tOrderDept = new String[2];
                        tOrderDept[0] = tVO.orgId;
                        tOrderDept[1] = tVO.orgName;
                        tList1.add(tOrderDept);
                        //取出送货地址封装到ArrayList容器中
                        String[] tDelGoodsAddr = new String[2];  
                        tList2 = new ArrayList();
                        tDelGoodsAddr[0] = tVO.deliverGoodsAddrId;
                        tDelGoodsAddr[1] = tVO.deliverGoodsAddrName;
                        tList2.add(tDelGoodsAddr);
                        //将订货单位编码赋值给临时变量用于判断下一个订货单位编码与当前编码是否相同
                        temp = tVO.orgId;
                        //将同一个订货单位对应的所有送货地址的ArrayList对象放入HashMap
                        tMap.put(String.valueOf(tVO.orgId),tList2);
                    }
                }
                //将封装了所有订货单位的ArrayList对象放入HashMap容器
                tMap.put("organization",tList1);
            }
        }catch(GSException ge){
            throw ge;
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E10064,GSException.E10064_MSG);
        }finally{
            GSOrderDeptInfoDAO.close(tConn);
        }
        return tMap;
    }
    /**
     * 巴士物资级操作员可操作订货单位及送货地址
     * @param aOrgId                    组织编码
     * @return
     * @throws GSException
     */
    public HashMap getOrderDeptInfoOfBusOper(String aOrgId) throws GSException{
        HashMap tMap = new HashMap();
        ArrayList tRs = new ArrayList();
        Connection tConn = null;
        GSOrderDeptInfoDAO tOrderDeptDAO = new GSOrderDeptInfoDAO();
        try{
            tConn = tOrderDeptDAO.getConnetion();
            
            //操作员属于巴士物资时，可操作所有运营公司及修理厂
            tRs = tOrderDeptDAO.queryAllOrderDeptInfoOfSubOrg(tConn);
            //查询操作员所属订货单位及送货地址
//            tRs.addAll(tOrderDeptDAO.queryOrderDeptInfoOfBusOperOrg(aOrgId,tConn));
            if(tRs.size() > 0){
                String temp = "";
                ArrayList tList1 = new ArrayList();
                ArrayList tList2 = null;
                for(int i=0; i<tRs.size(); i++){
                    GSOrderDeptInfoVO tVO = (GSOrderDeptInfoVO)tRs.get(i);
                    if(temp.equals(tVO.orgId)){
                        //取出同一个订货单位的不同送货地址封装到ArrayList容器中
                        String[] tDelGoodsAddr = new String[2];                        
                        tDelGoodsAddr[0] = tVO.deliverGoodsAddrId;
                        tDelGoodsAddr[1] = tVO.deliverGoodsAddrName;
                        tList2.add(tDelGoodsAddr);
                    }else{
                        //取出不同订货单位封装到ArrayList容器中
                        String[] tOrderDept = new String[2];
                        tOrderDept[0] = tVO.orgId;
                        tOrderDept[1] = tVO.orgName;
                        tList1.add(tOrderDept);
                        //取出送货地址封装到ArrayList容器中
                        String[] tDelGoodsAddr = new String[2];  
                        tList2 = new ArrayList();
                        tDelGoodsAddr[0] = tVO.deliverGoodsAddrId;
                        tDelGoodsAddr[1] = tVO.deliverGoodsAddrName;
                        tList2.add(tDelGoodsAddr);
                        //将订货单位编码赋值给临时变量用于判断下一个订货单位编码与当前编码是否相同
                        temp = tVO.orgId;
                        //将同一个订货单位对应的所有送货地址的ArrayList对象放入HashMap
                        tMap.put(String.valueOf(tVO.orgId),tList2);
                    }
                }
                //将封装了所有订货单位的ArrayList对象放入HashMap容器
                tMap.put("organization",tList1);
            }
        }catch(GSException ge){
            throw ge;
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E10064,GSException.E10064_MSG);
        }finally{
            GSOrderDeptInfoDAO.close(tConn);
        }
        return tMap;
    }    
    /**
     * 查询可操作的订货单位
     * @param aOrgId        当前操作员所在订货单位
     * @return
     * @throws GSException
     */
    public HashMap getOrderDeptOfBusOper(String aOrgId) throws GSException{
        HashMap tMap = new HashMap();
        ArrayList tRs = new ArrayList();
        Connection tConn = null;
        GSOrderDeptInfoDAO tOrderDeptDAO = new GSOrderDeptInfoDAO();
        try{
            tConn = tOrderDeptDAO.getConnetion();
			if(aOrgId.substring(0,2).equals("00")){
            //操作员属于巴士物资时，可操作所有运营公司及修理厂
			tRs = tOrderDeptDAO.queryAllOrderDeptInfoOfSubOrg(tConn);
			}else
			{
			//查询操作员所属订货单位及送货地址
			tRs = tOrderDeptDAO.queryOrderDeptInfoOfOrg(tConn,aOrgId);
			//查询操作员所属单位下级单位及送货地址
			tRs.addAll(tOrderDeptDAO.queryOrderDeptInfoOfOperSubOrg(tConn,aOrgId));//by sindy update 07-08-15
			}
            if(tRs.size() > 0){
                String temp = "";
                ArrayList tList1 = new ArrayList();
                for(int i=0; i<tRs.size(); i++){
                    GSOrderDeptInfoVO tVO = (GSOrderDeptInfoVO)tRs.get(i);
                    if(temp.equals(tVO.orgId)){
                        continue;
                    }else{
                        //取出不同订货单位封装到ArrayList容器中
                        String[] tOrderDept = new String[2];
                        tOrderDept[0] = tVO.orgId;
                        tOrderDept[1] = tVO.orgName;
                        tList1.add(tOrderDept);

                        //将订货单位编码赋值给临时变量用于判断下一个订货单位编码与当前编码是否相同
                        temp = tVO.orgId;
                    }
                }
                //将封装了所有订货单位的ArrayList对象放入HashMap容器
                tMap.put("organization",tList1);
            }
        }catch(GSException ge){
            throw ge;
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E10064,GSException.E10064_MSG);
        }finally{
            GSOrderDeptInfoDAO.close(tConn);
        }
        return tMap;
    }    
}
